package com.supersit.GZFDA.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.supersit.GZFDA.R;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

public class LoadDBData {
	private static SQLiteDatabase database;
	public static final String DATABASE_FILENAME = "GZFAD.db"; // �����DB�ļ�����
	public static final String PACKAGE_NAME = "com.supersit.GZFDA"; // ������Լ���Ŀ��·��
	public static final String DATABASE_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ PACKAGE_NAME + "/databases/"; // ��ȡ�洢λ�õ�ַ
	private static String databaseFilename = DATABASE_PATH + "/"
			+ DATABASE_FILENAME;

	public static SQLiteDatabase openDatabase(Context context) {
		try {
			File dir = new File(DATABASE_PATH);
			if (!dir.exists()) {
				dir.mkdir();
			}
			if (!(new File(databaseFilename)).exists()) {
				InputStream is = context.getResources().openRawResource(
						R.raw.gzfad);
				FileOutputStream fos = new FileOutputStream(databaseFilename);
				byte[] buffer = new byte[8192];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}
				fos.close();
				is.close();
			}
			database = SQLiteDatabase.openOrCreateDatabase(databaseFilename,
					null);
			return database;
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("tag", e.getMessage());
		}
		return null;
	}

	public static List<String> getBusiness_Name() {
		database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
		String sql = "select business_name from olddata";
		ArrayList<String> businessNameList = new ArrayList<String>();
		Cursor cursor = database.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			businessNameList.add(cursor.getColumnName(cursor
					.getColumnIndex("business_name")));
		}
		return businessNameList;

	}
}
